﻿/**
 * Created by lenovo on 2018/2/26.
 */

var phone = sessionStorage.getItem("userPhone");
$(function () {

    //和生活登录开始

    if (!phone) {
        //显示加载中
        layer.open({
            title: [
                '享动健身',
                'background-color:rgb(107, 164, 141); color:#fff;'
            ]
            , anim: 'up'
            , area: '70%'
            , offset: 'auto'
            , content: "登录中，请稍后……"
            , closeBtn: 0
            , btn: []
            , shadeClose: false
        });
    } else {
        GetUserTicket();
    }
    if (hsh.isApp) {
        if (!phone) {    //如果没有登录，请求和生活接口登录
            //hsh.loading();
            hsh.getTokenNew(function (responseData) {
                console.log(responseData);
                //alert(JSON.stringify(responseData));
                var token = responseData["token"];    //得到token
                 // var token = "1231"; 
                //和生活单点登录
                 $.post("/HeShengHuo/Login", { 'token': token }, GetLoginMethodResponse);

            });
        }
    }
    //----------和生活登录结束--------------


    submit();
    toVolume();
    toArea();
    volumeFrame();
    ruleFrame();


});

function GetLoginMethodResponse(data) {
    layer.closeAll();
    if (data.Success) {
        phone = data.phone;
        sessionStorage.setItem("userPhone", data.phone);
        sessionStorage.setItem("fuwuId", data.openid);   //保存fuwuId
        GetUserTicket();
		$.post("/HeShengHuo/UserDiscount", { 'phone': data.phone }, GetUserDiscountResponse);
    } else {
        ShowAlert(data.Message);
    }
}
function GetUserDiscountResponse(data){
	if(!data.Success)
		layer.msg(data.Message);
}


function GetUserTicket() {
    $.post("/HeShengHuo/GetUserRecTicket", { 'phone': phone }, GetUserRecTicketResponse);
}
function GetUserRecTicketResponse(data) {
    if (data.Success) {
        var result = data.Message;
        if (result.length > 0) {
            var str = "";
            for (var i = 0; i < result.length; i++) {
                var backImg = "background" + (result[i].Ticket.State == 1 ? result[i].Shop.ShopType.toString() : "hx" + result[i].Shop.ShopType) + ".png";
                str += '<div class="volume-list volume-list1" style="background:url(/image/shoptype/' + backImg+') no-repeat ;background-size: 100% 100%;" onclick="ShopDetails(' + result[i].Ticket.ShopId + ',' + result[i].Ticket.CurType + ')">';
                str += '<div class="logo"><img style="width: 100%; border-radius: 50%;" src="http://fuwu.xdjsclub.com/Shops/images/' + result[i].Shop.ImgLogo + '/logo.jpg" /></div>';
                str += '<div class="volume-info">';
                str += '<p class="name-info">' + result[i].Shop.ShopName + '(<span class="name-address">' + result[i].Shop.CityName + '</span>)</p>';
                str += '<p>卡号：<span>'+result[i].Ticket.TicketCode+'</span></p>';
                str += '<p class="volume-address"><img src="/image/index/place.png" alt="" /><span>' + result[i].Shop.UseShop + '</span></p>';
                str += '</div></div>';
            }
            $("#userTicket").html(str);
        }
    } else {
        ShowAlert(data.Message);
    }
}




//提交
function submit() {
    $(".submit-btn").click(function () {
        var val = $(".phone-number").val();
        console.log(val);
    })
}
//去各种体验馆
function toArea() {
    $(".free img").click(function () {
        window.location.href = "/HeShengHuo/FreeShop";
    });
    $(".years img").click(function () {
        window.location.href = "/HeShengHuo/YearsShop"
    });
    $(".current img").click(function () {
        window.location.href = "/HeShengHuo/CurrentShop"
    })
}
var ModalHelper = (function (bodyCls) {
    var scrollTop;
    return {
        afterOpen: function () {
            scrollTop = document.scrollingElement.scrollTop;
            document.body.classList.add(bodyCls);
            document.body.style.top = -scrollTop + 'px';
        },
        beforeClose: function () {
            document.body.classList.remove(bodyCls);
            // scrollTop lost after set position:fixed, restore it back.
            document.scrollingElement.scrollTop = scrollTop;
        }
    };
})('modal-open');
//弹出体验卷
function toVolume() {
    $(".my-btn").click(function () {
        $(".volume").animate({ bottom: '0' });
        ModalHelper.afterOpen();
    });
    $(".volume .close-img").click(function () {
        $(".volume").animate({ bottom: '-100%' })

        ModalHelper.beforeClose();
    })
}
//体验券详情弹出
function volumeFrame() {
    $(".volume .volume-list").click(function () {
        $(".volume-frame").show();
        ModalHelper.afterOpen();

    });
    $(".volume-frame .close-img").click(function () {
        $(".volume-frame").hide();
        ModalHelper.beforeClose();
    });
}
//活动规则弹出
function ruleFrame() {
    $(".ins-rule .rule-icon").click(function () {
        $(".rule-frame").show();
        ModalHelper.afterOpen();
    });
    $(".rule-frame .close-img").click(function () {
        $(".rule-frame").hide();
        //$("body,html").css({
        //    "height": "auto",
        //    "overflow": "auto"
        //})
        ModalHelper.beforeClose();
    })
}

